package org.snare.alumni.modules.app;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.snare.alumni.modules.admin.card.dto.CardDTO;
import org.snare.alumni.modules.admin.card.entity.CardEntity;
import org.snare.alumni.modules.admin.card.service.api.CardService;
import org.snare.alumni.modules.app.model.SchSnsCommentDTO;
import org.snare.alumni.modules.app.model.SchUserInfo;
import org.snare.community.modules.admin.account.service.api.SnsAccountService;
import org.snare.community.modules.admin.comment.dto.CommentQry;
import org.snare.community.modules.admin.comment.entity.CommentEntity;
import org.snare.community.modules.admin.comment.mapper.CommentMapper;
import org.snare.community.modules.admin.trend.dto.TrendDTO;
import org.snare.community.modules.admin.trend.dto.TrendQry;
import org.snare.community.modules.admin.trend.dto.TrendVO;
import org.snare.community.modules.admin.trend.service.api.TrendService;
import org.snare.community.modules.app.TrendController;
import org.snare.spider.cobweb.oss.dto.FileUploadDTO;
import org.snare.spider.common.base.Limit;
import org.snare.spider.common.utils.BeanUtils;
import org.snare.spider.service.annotation.ApiPost;
import org.snare.spider.toolkit.jwt.JwtUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "校友圈子")
@RequestMapping("/alumni/app/sns/trend")
@RestController
@RequiredArgsConstructor
public class SchTrendController {

    private final TrendController trendController;

    private final CardService cardService;

    private final TrendService trendService;

    private final SnsAccountService snsAccountService;

    private final CommentMapper commentMapper;

    @GetMapping("/page")
    @ApiOperation(value = "分页", httpMethod = "GET")
    public IPage<SchUserInfo> page(TrendQry params, Limit limit) {
        IPage<TrendVO> page = trendController.page(params, limit);
        IPage<SchUserInfo> iPage = BeanUtils.copy(page, SchUserInfo.class);
        for (SchUserInfo schUserInfo : iPage.getRecords()) {
            schUserInfo.setCardDTO(cardService.detail(null, schUserInfo.getUid()));
        }
        return iPage;
    }

    @ApiPost(mapping = {"/publish"}, desc = "发布")
    @Transactional
    public void publish(@RequestBody @Valid TrendDTO params) {
        String uid = JwtUtils.getUid();
        if (!cardService.checkApply(uid)) {
            throw new RuntimeException("请先申请校友卡获取更多权益");
        }
        params.setUid(uid);
        trendService.add(params);
    }

    @GetMapping("/detail")
    @ApiOperation(value = "详情", httpMethod = "GET")
    public SchUserInfo detail(String trendId) {
        SchUserInfo schUserInfo = BeanUtils.copy(trendController.detail(trendId), SchUserInfo.class);
        schUserInfo.setCardDTO(BeanUtils.copy(cardService.detail(null, schUserInfo.getUid()), CardDTO.class));
        return schUserInfo;
    }

    @GetMapping("/myself")
    @ApiOperation(value = "我的动态", httpMethod = "GET")
    public IPage<SchUserInfo> myself(Limit limit) {
        TrendQry params = new TrendQry();
        params.setUid(JwtUtils.getUid());
        return page(params, limit);
    }

    @GetMapping("/info")
    @ApiOperation(value = "个人信息", httpMethod = "GET")
    public Map<String, Object> info(String uid) {
        if (StringUtils.isBlank(uid)) {
            uid = JwtUtils.getUid();
        }
        CardDTO cardDTO = cardService.detail(null, uid);
        if (cardDTO != null) {
            cardDTO.setImgList(JSON.parseArray(cardDTO.getImages(), FileUploadDTO.class));
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("cardDTO", cardDTO);
        map.put("accountInfo", snsAccountService.info(uid));
        return map;
    }

    @GetMapping("/schCommentPage")
    @ApiOperation(value = "评论分页", httpMethod = "GET")
    public IPage<SchSnsCommentDTO> schCommentPage(CommentQry qry, Limit limit) {
        IPage<CommentEntity> page = new LambdaQueryChainWrapper<>(commentMapper)
                .eq(StringUtils.isNotBlank(qry.getTrendId()), CommentEntity::getTrendId, qry.getTrendId())
                .page(limit.page());
        IPage<SchSnsCommentDTO> iPage = BeanUtils.copy(page, SchSnsCommentDTO.class);
        for (SchSnsCommentDTO schSnsCommentDTO : iPage.getRecords()) {
            CardDTO fromUser = cardService.detail(null, schSnsCommentDTO.getFromUid());
            schSnsCommentDTO.setFromName(fromUser.getRealName());
            schSnsCommentDTO.setAuthor(schSnsCommentDTO.getFromName());
            schSnsCommentDTO.setBody(schSnsCommentDTO.getContent());
            schSnsCommentDTO.setAvatar(fromUser.getAvatar());
            // 回复评论
            if (StringUtils.isNotBlank(schSnsCommentDTO.getToUid())) {
                schSnsCommentDTO.setToName(cardService.detail(null, schSnsCommentDTO.getToUid()).getName());
                CommentEntity commentEntity = new LambdaQueryChainWrapper<>(commentMapper).eq(CommentEntity::getId, schSnsCommentDTO.getToId()).one();
                if (commentEntity != null) {
                    schSnsCommentDTO.setToContent(commentEntity.getContent());
                    schSnsCommentDTO.setBody(schSnsCommentDTO.getBody() + String.format("<p><blockquote>%s：%s</blockquote>", schSnsCommentDTO.getToName(), schSnsCommentDTO.getToContent()));
                }
            }
        }
        return iPage;
    }

}
